import { Button } from 'antd';
import { action, observable, toJS } from "mobx";
import * as React from 'react';
import DialogStore from ".";
import { WeaLocaleProvider } from "ecCom";
import Api from '../../apis/modetree'
const getLabel = WeaLocaleProvider.getLabel;

export default class AddSelectTreeStore extends DialogStore {

    @observable browserDatas = [];
    @observable type;
    @observable browserType;
    @observable browserTitile;
    @observable tabs;
    @observable datas;
    @observable browserInstance;
    @observable mainId;
    @observable dataParams;
    @observable beforeDrag;
    @observable draging = false;

    @observable datas = [];
    constructor() {
        super();
        this.style = { width: 520, height: 300 };
        this.title = getLabel('520405','固定解析维护');
        this.buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zfs6dm@cancel`} key="cancel" type="primary" onClick={this.hide}>{getLabel(31129, '取消')}</Button>];
    }
    action
    initOptions = (datas) => {

        if (datas) {
            datas = datas.filter(s => s.selectValue).map(d => ({ id: d.selectValue, name: d.selectName, pid: d.pid, nodeid: d.selectValue }));
            this.browserDatas = datas;
            Api.queryFormmodeTreeByIds({
                selectItem: JSON.stringify(datas),
                ...this.dataParams,
            }).then(this.setValue);
            return;
        } else {
            datas = [];
        }

        this.datas = datas.slice();
    }

    @action
    onChange = (datas) => {

        this.datas = datas.map((d, index) => ({
            //default: "",
            selectValue: index,
            //unuse: "",
            ...d
        }));
    }

    @action
    onDrag = (dragNode, targetNode, datas) => {
        console.log(dragNode, targetNode, datas);
        const oldValue = this.datas;
        this.datas = datas;
        if (dragNode.parentSuf == targetNode.parentSuf) {
            if (dragNode.parentSuf == "" || dragNode.parentPre == targetNode.parentPre) {
                return;
            }
        }
        this.datas = [];
        this.datas = oldValue;

    }

    @action
    ondelete = (delValue, o1, o2) => {
        this.datas = this.deleteTreeNode(delValue, this.datas);
        this.browserDatas = this.treeToList(this.datas);
    }

    deleteTreeNode = (deleteValue, datas) => {

        if (!datas)
            return;
        datas.forEach(d => {

            if (d.id == deleteValue.id) {
                datas.remove(d);
                return;
            }
            if (d.subs) {
                d.subs = this.deleteTreeNode(deleteValue, d.subs);

                if (!d.subs || d.subs.length == 0) {
                    d.isParent = false;
                }
            }

        })

        return datas;
    }

    @action
    setDragData = (dragData) => {
        this.datas = dragData;
        this.browserDatas = this.treeToList(this.datas);
    }

    @action
    treeToList = (tree) => {
        var queen = [];
        var out = [];
        queen = queen.concat(...tree.filter(r => {return r.deleted != true}));

        while (queen.length) {
            var first = queen.shift();
            if (first.subs) {
                queen = [...first.subs].concat(queen)
                //delete first['subs'];
            }
            if (first.deleted) {

                continue;
            }
            out.push(first);
        }
        return out;
    }

    @action
    showBrowserModal = () => {
        if (this.browserInstance) {
            this.browserInstance.openModal();
        }
    }

    @action
    changeValue = (newValue, valueSpan, valueObj) => {

        //this.datas = valueObj;
        //const value = valueObj.map(d => ({ selectValue: d.id, randomkey: d.id, selectName: d.name }));
        Api.queryFormmodeTreeByIds({
            selectItem: JSON.stringify(valueObj),
            ...this.dataParams,
        }).then(this.setValue);
        //this.setValue(valueObj);
    }


    @action
    setValue = (value) => {

        this.datas = value;
    }

    @action
    browserSetting = () => {
        const range = this.sheet.getCell(this.row, this.col);
        this.type = range.cellType().fieldType;
        console.log(this.type)
        this.browserType = this.type[1].value;
        if (this.type.length == 2) {

            this.browserTitile = this.type[1].valueSpan;
        } else {
            if (this.type[1]["valueObj"]) {
                this.browserTitile = this.type[1]["valueObj"][0].name;
            } else {
                this.browserTitile = this.type[1].valueSpan;
            }

        }
        // 由于单选都需要多选，所以需要进行类型转换
        if (this.browserType == 1)
            this.browserType = 17;
        if (this.browserType == 4)
            this.browserType = 57;
        if (this.browserType == 7)
            this.browserType = 18;
        if (this.browserType == 8)
            this.browserType = 135;
        if (this.browserType == 9)
            this.browserType = 37;
        if (this.browserType == 87)
            this.browserType = 184;
        if (this.browserType == 161)
            this.browserType = 162;
        if (this.browserType == 164)
            this.browserType = 194;
        if (this.browserType == 165)
            this.browserType = 166;
        if (this.browserType == 167)
            this.browserType = 168;
        if (this.browserType == 169)
            this.browserType = 170;
        if (this.browserType == 256)
            this.browserType = 257;
        if (this.browserType == '161' || this.browserType == '162'){
            this.dataParams = { ...this.dataParams, type: (this.type[2] || {}).value }
        }else if(this.browserType == '256' || this.browserType == '257') {
            this.dataParams = { ...this.dataParams, cube_treeid: (this.type[2] || {}).value }
        } else if(this.browserType == '171'){
            this.dataParams = {...this.dataParams, status:2};
        }
        if (this.browserType == '4' || this.browserType == '57' || this.browserType == '167' || this.browserType == '168') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else if (this.browserType == '164' || this.browserType == '169' || this.browserType == '170' || this.browserType == '194') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else {
            this.tabs = {};
        }
    }
}